<?php
    /**
     * @ignore
     */
    if( defined("apl_".__FILE__)) return;
    define("apl_".__FILE__,"apl_".__FILE__);

    class AplFileUpload
    {
        private $Name;

        public function setName( $postName )
        {
            $this->Name = $postName;
        }

        public function getName()
        {
            return $this->Name;
        }
        protected function tmpName()
        {
            $this->ensureFileExists();
            return $_FILES[$this->Name]["tmp_name"];
        }
        protected function ensureFileExists()
        {
            if( !$this->hasFile() )
                throw new Exception("File not exists!");
        }

        public function hasFile()
        {
            return isset($_FILES[$this->Name]);
        }

        public function fileType()
        {
            $this->ensureFileExists();
            return $_FILES[$this->Name]["type"];
        }

        public function fileName()
        {
            $this->ensureFileExists();
            return $_FILES[$this->Name]["name"];
        }

        public function fileSize()
        {
            $this->ensureFileExists();
            return $_FILES[$this->Name]["size"];
        }
        
        public function fileExtension()
        {
            $this->ensureFileExists();
            $fileInfo = pathinfo($this->fileName() );
            return $fileInfo["extension"];
        }

        public function saveAs( $newPath )
        {
            $this->validateUpload();
            return move_uploaded_file( $this->tmpName(), $newPath );
        }

        protected function validateUpload()
        {
            if( !is_uploaded_file( $this->tmpName() ) )
                $this->failValidate();
                
        }
        protected function failValidate()
        {
                throw new Exception("Not a valid upload!");
        }
    }
?>